package com.java.ttt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.java.ttt.bo.User;

public class CreateBoardDao{

	public int createBoard(User player1){
		
		//sql += " username = ? AND password = ?";
		Connection conn = null;
		PreparedStatement ps = null;

		try {
			
			conn = BaseDbConnection.getConnection();
			String sql = "insert into ttt3.ActiveGames (Player1, isFull) values (?, 'N')";
			
			ps =  conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			ps.setLong(1, player1.getUserId());
			
			int row = ps.executeUpdate();
			if (row != 1) return 0;
			ResultSet keys = ps.getGeneratedKeys();    
			keys.next();  
			int key = keys.getInt(1);
			
			System.out.println("Active Game:"+ key);
			
			return key;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			
			try {
				if (ps != null)
					ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/*try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
			
		}
	}
}
